﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.BusinessLogicLayer;
public partial class Report_VideoViewReport : AdminUserBase
{
    VideoCommentModule vm = new VideoCommentModule();
    string sqlwhere = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData("");
            BindTrainClassData();
            BindCourseData();
        }
    }
    public void BindData(string sqlwhere)
    {
        DataSet ds = new DataSet();
        ds = vm.GetUserViewVideoReport(sqlwhere, this.SchoolID);
        Repeater1.DataSource = getPage(ds);
        Repeater1.DataBind();
        
    }
    public PagedDataSource getPage(DataSet ds)
    {
        this.pager.RecordCount = ds.Tables[0].Rows.Count;
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = pager.CurrentPageIndex - 1;
        pds.PageSize = pager.PageSize;
        return pds;
    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        BindData(sqlwhere);

    }
    public void BindTrainClassData()
    {
        int id = this.CurrentUser.SchoolID;
        TrainClassModule tcm = new TrainClassModule();
        DataSet ds = tcm.GetTrainclassListByWhere(id, "");
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = null;
            ddlTrainClass.Items.Clear();
            ddlTrainClass.Items.Add(new ListItem("全部培训班", "0"));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = ds.Tables[0].Rows[i];
                ddlTrainClass.Items.Add(new ListItem(dr["VTC_name"].ToString(), dr["ID"].ToString()));
            }

        }
    }
    public void BindCourseData()
    {
        int id = this.CurrentUser.SchoolID;
        CourseModule tcm = new CourseModule();
        DataSet ds = new DataSet();
        if (this.ddlTrainClass.SelectedValue != "0" && ddlTrainClass.SelectedValue!="")
        {
            ds = tcm.GetCourseListByWhere(id, int.Parse(this.ddlTrainClass.SelectedValue), "");
        }
        else
        {
            ds = tcm.GetCourseListByWhere(id, "");
        }
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = null;
            ddlCourse.Items.Clear();
            ddlCourse.Items.Add(new ListItem("全部课程", "0"));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = ds.Tables[0].Rows[i];
                ddlCourse.Items.Add(new ListItem(dr["VC_name"].ToString(), dr["ID"].ToString()));
            }

        }
    }
    protected void btnCheckByTrainClass_Click(object sender, EventArgs e)
    {
        int trainclassid =Convert.ToInt32(ddlTrainClass.SelectedValue);
        int courseid = Convert.ToInt32(ddlCourse.SelectedValue);
        if (txtBeginDate.Text == "" || txtendDate.Text == "")
        {
            if (ddlCourse.SelectedValue == "0" && ddlTrainClass.SelectedValue == "0")
            { sqlwhere = ""; }
            else if (ddlCourse.SelectedValue == "0" && ddlTrainClass.SelectedValue != "0")
            { sqlwhere = " and vc.trainClassID=" + trainclassid.ToString(); }
            else if (ddlCourse.SelectedValue != "0" && ddlTrainClass.SelectedValue== "0")
            { sqlwhere = " and vc.courseID=" + courseid.ToString(); } 
            else if (ddlCourse.SelectedValue != "0" && ddlTrainClass.SelectedValue != "0")
            { sqlwhere = " and vc.trainClassID=" + trainclassid.ToString() + " and vc.courseID=" + courseid.ToString(); } 

        }
        else
        {
            DateTime beginDateTime =Convert.ToDateTime( txtBeginDate.Text.Trim());
            DateTime endDateTime = Convert.ToDateTime(txtendDate.Text.Trim());
            if (ddlCourse.SelectedValue == "0" && ddlTrainClass.SelectedValue == "0")
            { sqlwhere = " and VCL_startDate between '" + beginDateTime + "' and '" + endDateTime + "'";  }
            else if (ddlCourse.SelectedValue == "0" && ddlTrainClass.SelectedValue != "0")
            { sqlwhere = " and VCL_startDate between '" + beginDateTime + "' and '" + endDateTime + "' and vc.trainClassID=" + trainclassid.ToString(); }
            else if (ddlCourse.SelectedValue != "0" && ddlTrainClass.SelectedValue == "0")
            { sqlwhere = " and VCL_startDate between '" + beginDateTime + "' and '" + endDateTime + "' and vc.courseID=" + courseid.ToString(); } 
            else if (ddlCourse.SelectedValue != "0" && ddlTrainClass.SelectedValue != "0")
            { sqlwhere = " and VCL_startDate between '" + beginDateTime + "' and '" + endDateTime + "' and vc.trainClassID=" + trainclassid.ToString() + " and vc.courseID=" + courseid.ToString(); } 

        }

        BindData(sqlwhere);
    }
    protected void ddlTrainClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindCourseData();
    }
}
